package com.MusicWeb.servlet;

import com.MusicWeb.entity.Tip;
import com.MusicWeb.entity.User;
import com.MusicWeb.service.TipService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/tipadd")
//todo : 用户发送短消息  传入用户id 音乐id 标题 内容
//http://localhost:8080/tipadd?title=?&text=?&uid=?&mid=?
public class TipAddServlet extends HttpServlet {
    TipService tipService = new TipService();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setCharacterEncoding("UTF-8");
        req.setCharacterEncoding("UTF-8");
        Integer uid = Integer.parseInt(req.getParameter("uid"));
        Integer mid = Integer.parseInt(req.getParameter("mid"));
        Integer from_id = Integer.parseInt(req.getParameter("from_id"));

        User user = (User) req.getSession().getAttribute("user");
//        String name=user.getName();
//        Integer from_id=user.getId();
        String title = req.getParameter("title");
        String text = req.getParameter("text");


        Tip tip = new Tip();
        tip.setText(text);
        tip.setMid(mid);
        tip.setUid(uid);
        tip.setTitle(title);
        tip.setFrom_id(from_id);

//        tip.setName(name);


        if (tip!=null&&tipService.TipAdd(tip) != -1) {

            req.getSession().setAttribute("msg", "发送成功");
            resp.sendRedirect(req.getContextPath() + "/FunctionPage/MusicHome.jsp");
        } else {
            req.getSession().setAttribute("msg", "发送失败");
            resp.sendRedirect(req.getContextPath() + "/FunctionPage/MusicHome.jsp");
        }
    }
}
